<template>
	<div class="system-menu-dialog-container">
		<el-dialog :rules="rules" :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="menuDialogFormRef" :model="ruleForm" size="default" label-width="120px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="型号编号：" prop="number" :rules="rules.number" :label-width="200">
							<el-input size="default"  placeholder="型号编号" style="max-width: 400px" v-model="ruleForm.number"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="设备型号：" prop="name" :rules="rules.name" :label-width="200">
							<el-input size="default"  placeholder="设备型号" style="max-width: 400px" v-model="ruleForm.name"> </el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="设备类型" prop="type" :rules="rules.type" :label-width="200">
							<el-select clearable v-model="ruleForm.type" placeholder="设备类型" style="max-width: 400px">
                                <el-option label="MEC" :value="1"></el-option>
                                <el-option label="RSU" :value="2"></el-option>
                                <el-option label="近景相机" :value="3"></el-option>
                                <el-option label="远景相机" :value="4"></el-option>
                                <el-option label="鱼眼相机" :value="5"></el-option>
                                <el-option label="激光雷达" :value="6"></el-option>
                                <el-option label="毫米波雷达" :value="7"></el-option>
                                <el-option label="信号灯采集器" :value="8"></el-option>
                                <el-option label="RTK" :value="9"></el-option>
						    </el-select>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="厂商名称：" prop="manufacturer_id" :rules="rules.manufacturer_id" :label-width="200">
							<el-select clearable filterable v-model="ruleForm.manufacturer_id" placeholder="厂商名称" style="max-width: 400px">
                                <el-option v-for="item in selectData.data" :key="item.id" :label="item.name" :value="item.id"></el-option>
						    </el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="设备版本:" prop="device_version"  :label-width="200">
							<el-input size="default"  placeholder="设备版本" style="max-width: 400px" v-model="ruleForm.device_version"> </el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemMenuDialog">
import { defineAsyncComponent, reactive, onMounted, ref,watch,defineProps } from 'vue';
import {  ElMessage, type FormRules } from 'element-plus'
import {GenreAPI} from "/@/api/fun/genre/index"
import {MansAPI} from "/@/api/fun/manufacturer/index"
const ManApi = MansAPI()
const GenApi = GenreAPI()

const props = defineProps(['tableData',"disabled"])
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const disabledFlag = ref<Boolean>(false)

// dx定义start

	interface RuleForm {
		number:string,
		name:string,
		type:string,
		manufacturer_id:string,
        device_version:string
	}
	const ruleForm = ref({
		number:"",
		name:"",
		type:"",
		manufacturer_id:"",
        device_version:""
	})
	
	watch(props,(newVal,oldVal)=>{
		ruleForm.value = newVal.tableData
		disabledFlag.value = newVal.disabled
	})
	const rules = reactive<FormRules<RuleForm>>({
		number:[
			{ required: true, message: '型号编号必填', trigger: 'blur' },
		],
		name:[
			{ required: true, message: '设备型号必填', trigger: 'blur' },
		],
		type:[
			{ required: true, message: '设备类型必填', trigger: 'blur' },
		],
		manufacturer_id:[
			{ required: true, message: '厂商名称必填', trigger: 'blur' },
		],
		
	})

    const selectData =reactive({
    data:[],
})
const searchForm = ref({search:""})
const getSelectData = (async ()=>{
	searchForm.value.search = `status:1`
	let res = await ManApi.getTableList();
	if(res.isSuccess){
		selectData.data = res.returnData
	}
})






// dx定义end
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});


// 打开弹窗
const openDialog = () => {
	
		state.dialog.title = '修改设备类型';
		state.dialog.submitTxt = '修 改';
	
	state.dialog.isShowDialog = true;
};
const menuDialogFormRef = ref(null)
// 关闭弹窗
const closeDialog = () => {
	// menuDialogFormRef.value.resetFields()
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = (async() => {
	
		const res = await GenApi.editGen(ruleForm.value);
		if(res.isSuccess){
			ElMessage.success('编辑成功')
			closeDialog(); // 关闭弹窗
			emit('refresh');
		}else{
			ElMessage.error(res.returnMsg)
		}
});
// 页面加载时
onMounted(() => {
    getSelectData()
});

// 暴露变量
defineExpose({
	openDialog,
});
</script>
